<template>
	<div class="tikuBg">
		<div class="limitBox" v-if="!limitStatus">
			<div class="formBox" v-if="!nolimit">
				<h3>面试题库登录</h3>
				<p>雪恒教育-白雪面试内部专用</p>
				<div class="btn"><u-button type="error" text="点击登录" @click="toLogin()"></u-button></div>
			</div>
			<div class="formBox" v-else>
				<h3>您暂无权限，请联系教务老师给您添加权限</h3>
			</div>
		</div>
		<div class="banner">
			<image src="https://www.baixuemianshi.com/zhuanti/images/swiper_xcx2.jpg" mode="widthFix"></image>
		</div>
		<div class="banjiList">
			<h3>雪恒白雪-题目信息</h3>
			<ul class="btnList">
				<li @click="changePath('./xbjnr_ls')">
					<h3>小白姐核心题</h3>
				</li>
				<li v-for="(bj,index) in userinfo" :key="index" @click="changeBj(bj.banji)">
					<h3>{{ bj.banji }}</h3>
				</li>
				<li @click="changePath('./zjtb')">
					<h3>自检题本</h3>
				</li>
			</ul>
			<h3>雪恒白雪-学员服务</h3>
			<ul class="btnList">
				<li @click="changePath('../pingfen/readOnly')">
					<h3>学员模考得分情况</h3>
				</li>
				<li @click="changePath('../qingjia/shenhe')">
					<h3>学员请假审批</h3>
				</li>
				
				<li @click="changePath('../pingfen/zbls')">
					<h3>评分-中班老师</h3>
				</li>
			</ul>
		</div>
		
		<div class="loadBox" v-if="searchLoading">
			<u-loading-icon size="36" mode="circle" :vertical="true" color="red" text="登录中..." textSize="20"></u-loading-icon>
		</div>
		<canvas canvas-id="waterMark" class="waterMark" style="width: 100vw; height: 100vh;"></canvas>
		<image :src="waterMarkUrl" class="waterMark-bg" style="width: 100vw; height: 100vh;"></image>
	</div>
</template>

<script>
	import {
		mapActions
	} from 'vuex';
	const db = wx.cloud.database()
	export default {
		data() {
			return {
				userinfo: {},
				nolimit: false,
				limitStatus:false,
				
				waterMarkUrl: "",
				windowWidth: 100,
				windowHeight: 100,
				hasXb:false,
				searchLoading: true
			}
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				return {
					title: `题库-学员端`, //分享标题
					path: '/pages/nbhd/tiku/student', //分享页面路径
					imageUrl: 'https://www.baixuemianshi.com/zhuanti/tiku/images/xcx_share.jpg', //分享图标
					desc: '雪恒教育-白雪面试', //自定义分享描述 
				}
			} else {
				return {
					title: `题库-学员端`, //分享标题
					path: '/pages/nbhd/tiku/student', //分享页面路径
					imageUrl: 'https://www.baixuemianshi.com/zhuanti/tiku/images/xcx_share.jpg', //分享图标
					desc: '雪恒教育-白雪面试', //自定义分享描述 
				}
			}
		},
		onLoad(options) {
			
		},
		onShow() {
			if(wx.setVisualEffectOnCapture){
			    wx.setVisualEffectOnCapture({
			        visualEffect: 'hidden',
			        success:(res) => {
			            console.log('success回调',res)
			        },
			    })
			}
		},
		onHide() {
			if(wx.setVisualEffectOnCapture){
			    wx.setVisualEffectOnCapture({
			        visualEffect: 'none',
			        success:(res) => {
			            console.log('success回调',res)
			        },
			    })
			}
		},
		computed: {
			userphone() {
				return this.$store.getters.userphone ? this.$store.getters.userphone : '';
			},
			selectBanji(){
				return this.$store.getters.selectBanji ? this.$store.getters.selectBanji : '';
			}
		},
		created() {
			let that = this;
			
		},
		mounted() {
			let that = this 
			if(that.userphone){
				that.getuserStatus()
			}else{
				that.searchLoading = false
			}
			wx.hideLoading()
			
			if(this.userphone){
				this.drawWaterMark('雪恒教育-白雪面试 ' + this.userphone);
			}else{
				this.drawWaterMark('雪恒教育-白雪面试');
			}
		},
		watch: {
			userphone(val){
				if (this.userphone) {
					this.getuserStatus()
				}
			}
		},
		methods: {
			...mapActions(['LogSetPhone','LogSetBanji']),
			getHasZb(){
				let that = this
				db.collection('24gk_pingfen').where({
					zbls: that.userinfo[0].teacher
				}).get({
					success: res =>{
						if(res.data.length > 0){
							that.hasXb = true
						}
					}
				})
			},
			drawWaterMark(text) {
				this.getDeviceInfo().then(response => {
					const w = response.windowWidth;
					const h = response.windowHeight;
					this.windowWidth = w;
					this.windowHeight = h;
					//创建canvas绘制上下文
					const canvas = uni.createCanvasContext("waterMark", this);
					canvas.clearRect(0, 0, w, h)
					//开始创建一个路径，需要调用fill或者stroke才会使用路径进行填充或描边。
					canvas.beginPath();
					canvas.setFontSize(18);
					canvas.setFillStyle('rgba(0, 0, 0, 0.1)');
					canvas.rotate(-30 * Math.PI / 180)
					if (text) {
						text = this.trim(text)
					}
					const maxWidth = text.length * 18
					for (let x = 0; x < 10; x++) {
						for (let y = 0; y < 20; y++) {
							//在画布上绘制被填充的文本。
							canvas.fillText(text, (maxWidth + 40) * x - 400, 90 * y + 20);
						}
					}
					//将之前在绘图上下文中的描述（路径、变形、样式）画到 canvas 中。
					canvas.draw(false, () => {
						//把当前画布指定区域的内容导出生成指定大小的图片
						uni.canvasToTempFilePath({
							canvasId: "waterMark",
							complete: (res) => {
								this.waterMarkUrl = res.tempFilePath
							}
						}, this)
					})
				});
			},
			trim(str) {
				let trimStr = '';
				const reg = /[\t\r\f\n\s]*/g;
				if (typeof str === 'string') {
					trimStr = str.replace(reg, '');
				}
				return trimStr
			},
			/**
			 * 获取当前设备的信息
			 * **/
			getDeviceInfo() {
				return new Promise((resolve, reject) => {
					uni.getSystemInfo({
						success: (sysInfo) => {
							resolve(sysInfo)
						}
					})
				})
			},
			toLogin(){
				uni.navigateTo({
					url: '../../my/login'
				})
			},
			getuserStatus(){
				let that = this
				db.collection('nbhd_tiku_banjilist').where({
					phone: that.userphone
				}).get({
					success: function(res){
						if(res.data.length > 0){
							that.userinfo = res.data
							uni.showToast({
								title: '登录成功',
								icon: 'success'
							})
							that.getHasZb()
							
							that.limitStatus = true
						}else{
							uni.showToast({
								title: '暂无权限',
								icon: 'error'
							})
							that.nolimit = true
						}
						that.searchLoading = false
					},
					fail: function(req){
						uni.showToast({
							title: '暂无权限',
							icon: 'error'
						})
						that.searchLoading = false
					}
				})
			},
			changeBj(bj){
				this.LogSetBanji(bj)
				uni.navigateTo({
					url:'./selectMoshi'
				})
				
			},
			changePath(path){
				let that= this
				uni.navigateTo({
					url: path
				})
			}
		},
		components: {
			
		}
	};
</script>

<style scoped>
	@import url(./index.css);
	
</style>